<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      @submit.native.prevent
    >
      <el-form-item label="" prop="tableName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入编码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
        </el-button>
      </el-form-item>
    </el-form>

    <div :class="tableMain" v-show="tableMainShow">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增portal</el-button
          >
        </el-col>
        <el-col :span="1.5" style="float: right">
          <el-button
            size="mini"
            @click="tableExtend(1)"
            icon="el-icon-full-screen"
          >
          </el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableList"
        stripe
        @selection-change="handleSelectionChange"
        :header-cell-style="{ background: '#F6F8FB' }"
        @row-click="(row) => checkColumn(row.id, row.name)"
        :height="maxHeight"
        v-scrollbar="'.el-table__body-wrapper'"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="ID" align="center" prop="id" sortable />
        <el-table-column label="编码" align="center" prop="name" sortable />
        <el-table-column
          label="描述"
          align="center"
          prop="description"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="状态"
          align="center"
          prop="status"
          :formatter="statusFormat"
        ></el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="120"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <div class="operate-group">
              <span
                style="cursor: pointer; color: #409eff; font-size: 12px"
                type="text"
                class="el-icon-edit"
                :plain="false"
                @click="handleUpdate(scope.row)"
                >编辑</span
              >
              <span
                style="cursor: pointer; color: #409eff; font-size: 12px"
                type="text"
                class="el-icon-delete"
                :plain="false"
                @click="handleDelete(scope.row.id)"
                >删除</span
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <resize-bar v-show="tableMainShow && tableDependShow" />
    <div :class="tableDepend" v-show="tableDependShow">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="columnAdd"
            >添加Channel</el-button
          >
        </el-col>
        <el-col :span="1.5" style="float: right">
          <el-button
            size="mini"
            @click="tableExtend(2)"
            icon="el-icon-full-screen"
          >
          </el-button>
        </el-col>
        <div class="tableDependTitle">{{ tableDependName }} 的portal区域</div>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableDependList"
        stripe
        @selection-change="handleSelectionChange"
        :header-cell-style="{ background: '#F6F8FB' }"
        :height="maxHeight"
        v-scrollbar="'.el-table__body-wrapper'"
      >
        <el-table-column label="属性名称" align="center" prop="name" />
        <el-table-column label="描述" align="center" prop="description" />
        <el-table-column label="顺序号" align="center" prop="columnno" />
        <el-table-column
          label="状态"
          align="center"
          prop="status"
          :formatter="statusFormat"
        />
        <el-table-column
          label="操作"
          align="center"
          width="120"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <div class="operate-group">
              <span
                style="cursor: pointer; color: #409eff; font-size: 12px"
                type="text"
                class="el-icon-edit"
                :plain="false"
                @click="
                  columnUpdate(
                    scope.row.id,
                    scope.row.columnno,
                    scope.row.lineno
                  )
                "
                >编辑</span
              >
              <span
                style="cursor: pointer; color: #409eff; font-size: 12px"
                type="text"
                class="el-icon-delete"
                :plain="false"
                @click="columnDelete(scope.row.id)"
                >删除</span
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="dependTotal > 0"
        :total="dependTotal"
        :page.sync="queryDependParams.pageNum"
        :limit.sync="queryDependParams.pageSize"
        @pagination="getColumnList"
      />
    </div>
    <!-- 添加或修改table表单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      v-dialogDrag
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        @submit.native.prevent
      >
        <el-form-item label="编码" prop="name">
          <el-input v-model="form.name" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio key="1" label="1">正常</el-radio>
            <el-radio key="0" label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addOpen"
      width="600px"
      append-to-body
      v-dialogDrag
      :close-on-click-modal="false"
    >
      <el-table
        v-loading="loading"
        :data="commandList"
        height="480px"
        stripe
        @selection-change="handleSelectionCommand"
        :header-cell-style="{ background: '#F6F8FB' }"
        v-scrollbar="'.el-table__body-wrapper'"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="描述" align="center" prop="description" />
        <el-table-column label="所属行" align="center" prop="row" />
        <el-table-column label="所属列" align="center" prop="col" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCommandForm">确 定</el-button>
        <el-button @click="cancelCommand">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改table列对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="columnOpen"
      width="600px"
      append-to-body
      v-dialogDrag
      :close-on-click-modal="false"
    >
      <el-form
        ref="columnForm"
        :model="columnForm"
        label-width="80px"
        @submit.native.prevent
      >
        <el-form-item label="显示顺序" prop="columnno">
          <el-input-number
            v-model="columnForm.columnno"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitColumnForm">确 定</el-button>
        <el-button @click="cancelColumn">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDataList,
  getData,
  addData,
  updateData,
  delData,
  getColumnLists,
  updateColumn,
} from "@/api/pms/dynamic";

export default {
  inject: ["app"],
  name: "Portal",
  components: {
    resizeBar: () => import("@/views/components/resizeBar"),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      commandIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      dependTotal: 0,
      tableMain: "table-main",
      tableDepend: "table-depend",
      tableMainShow: true,
      tableDependShow: true,
      // 表格数据
      tableList: [],
      tableDependList: [],
      commandList: [],
      tableDependName: "",
      fromId: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      columnOpen: false,
      addOpen: false,
      route: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.$store.state.user.pageSize||30,
        name: undefined,
      },
      queryDependParams: {
        pageNum: 1,
        pageSize: this.$store.state.user.pageSize||30,
      },
      // 表单参数
      form: {},
      columnForm: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.route = "/pms/" + this.$route.name.split("/")[0].toLowerCase();
    this.getList();
  },
  computed: {
    maxHeight() {
      return this.app.screenHeight - 270;
    },
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getDataList(this.route, this.queryParams).then((response) => {
        this.tableList = response.rows;
        this.total = response.total;
        if (this.tableList.length > 0) {
          this.fromId = response.rows[0].id;
          this.tableDependName = response.rows[0].name;
          this.getColumnList();
        } else {
          this.tableDependList = [];
          this.dependTotal = 0;
        }
        this.loading = false;
      });
    },
    getColumnList() {
      if (this.fromId) {
        this.loading = true;
        this.queryDependParams.portalid = this.fromId;
        getColumnLists("/pms/portalarea", this.queryDependParams).then(
          (response) => {
            this.tableDependList = response.rows;
            this.dependTotal = response.total;
            this.loading = false;
          }
        );
      }
    },
    checkColumn(id, name) {
      this.tableDependName = name;
      this.fromId = id;
      this.getColumnList();
    },
    // 表单重置
    reset() {
      this.form = { status: "1" };
      this.resetForm("form");
      this.resetForm("columnForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 状态翻译
    statusFormat(row, column) {
      if (row.status == 1) {
        return "正常";
      } else {
        return "禁用";
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加Portal";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const Id = row.id || this.ids;
      getData(this.route, Id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改table表单";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateData(this.route, this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addData(this.route, this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(Id) {
      const Ids = Id || this.ids;
      this.$confirm(
        '是否确认删除Portal编号为"' +
          (Array.isArray(Ids) && Ids.length > 2 ? Ids[0] + "等" : Ids) +
          '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delData("/pms/portal", Ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 新增列按钮操作 */
    columnAdd() {
      if (!this.fromId) {
        this.msgError("请选择Portal区域");
        return false;
      }
      this.loading = true;
      getDataList("/pms/portalarea").then((response) => {
        this.commandList = response.rows;
        this.loading = false;
      });
      this.addOpen = true;
      this.title = "添加portal区域";
    },
    //Command多选选中数据
    handleSelectionCommand(selection) {
      this.commandIds = selection.map((item) => item.id);
    },
    /** Command提交按钮 */
    submitCommandForm: function () {
      if (this.fromId != undefined && this.commandIds.length > 0) {
        var commandForm = {};
        commandForm.portalid = this.fromId;
        commandForm.id = this.commandIds.join(",");
        updateData("pms/portalarea/link", commandForm).then((response) => {
          if (response.code === 200) {
            this.msgSuccess("添加portal区域成功");
            this.addOpen = false;
            this.getColumnList();
          }
        });
      } else {
        this.msgError("参数错误！");
      }
    },
    // 添加命令取消按钮
    cancelCommand() {
      this.addOpen = false;
      this.commandIds = [];
    },
    /** 修改列按钮操作 */
    columnUpdate(id, columnno, lineno) {
      this.columnForm.id = id;
      this.columnForm.columnno = columnno;
      this.columnForm.lineno = lineno;
      this.columnOpen = true;
      this.title = "修改Portal列";
    },
    // 列取消按钮
    cancelColumn() {
      this.columnOpen = false;
      this.columnForm = {};
    },
    /** 列提交按钮 */
    submitColumnForm: function () {
      this.$refs["columnForm"].validate((valid) => {
        if (valid) {
          if (this.columnForm.id != undefined) {
            updateColumn("/pms/subcommand", this.columnForm).then(
              (response) => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.resetForm("columnForm");
                  this.columnOpen = false;
                  this.getColumnList();
                }
              }
            );
          }
        }
      });
    },
    /** 删除列按钮操作 */
    columnDelete(Id) {
      const Ids = Id;
      this.$confirm(
        '是否确认删除命令编号为"' +
          (Array.isArray(Ids) && Ids.length > 2 ? Ids[0] + "等" : Ids) +
          '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          var commandForm = {};
          commandForm.portalid = 0;
          commandForm.id = Ids;
          return updateData("pms/portalarea/link", commandForm);
        })
        .then(() => {
          this.getColumnList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    //全屏/缩小
    tableExtend(type) {
      if (type === 2) {
        if (this.tableDepend === "table-depend") {
          this.tableMainShow = false;
          this.tableDepend = "table-extend";
        } else {
          this.tableMainShow = true;
          this.tableDepend = "table-depend";
        }
      } else {
        if (this.tableMain === "table-main") {
          this.tableDependShow = false;
          this.tableMain = "table-extend";
        } else {
          this.tableDependShow = true;
          this.tableMain = "table-main";
        }
      }
    },
  },
};
</script>
